package com.example.lib_base

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import android.view.LayoutInflater
import android.view.Window
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.therouter.TheRouter
import java.lang.reflect.ParameterizedType

/**
 * Activity基类
 */
abstract class BaseActivity<T : ViewDataBinding> : AppCompatActivity() {
    var TAG = this::class.java.simpleName

    lateinit var binding: T

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setFullscreen()
        TheRouter.inject(this);

        // 反射创建 binding对象 和 viewModel对象
        val type = (this.javaClass.genericSuperclass as ParameterizedType).actualTypeArguments
        val vbClass: Class<T> = type[0] as  Class<T>
        val method = vbClass.getDeclaredMethod("inflate", LayoutInflater::class.java)
        binding = method.invoke(this, layoutInflater) as T
        setContentView(binding.root)

        binding.lifecycleOwner = this
        ViewCompat.setOnApplyWindowInsetsListener(binding.root) { view, windowInsets ->
            val insets = windowInsets.getInsets(WindowInsetsCompat.Type.statusBars())
            view.setPadding(0,insets.top,0,getNavigationBarHeight(this))
            WindowInsetsCompat.CONSUMED
        }
        onViewInit()
    }

    private fun setFullscreen() {
        // 设置沉浸状态栏
        window.statusBarColor = Color.TRANSPARENT
        WindowCompat.setDecorFitsSystemWindows(window, false)
        window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        // 去除标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        // 隐藏标题栏
        supportActionBar?.hide()
        setNavigationStatusColor()
    }

    private fun setNavigationStatusColor() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            //当背景透明时去掉灰色蒙层
            window.isNavigationBarContrastEnforced = false
        }
        //导航栏背景颜色透明
        window.navigationBarColor = Color.TRANSPARENT
    }

    /**
     * This method can be implemented by subclasses for additional initialization.
     */
    abstract fun onViewInit()

    override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
        super.onSaveInstanceState(outState, outPersistentState)
    }

    override fun onRestoreInstanceState(
        savedInstanceState: Bundle?,
        persistentState: PersistableBundle?
    ) {
        super.onRestoreInstanceState(savedInstanceState, persistentState)
    }

    fun getNavigationBarHeight(context: Context): Int {
        val rid: Int =
            context.resources.getIdentifier("config_showNavigationBar", "bool", "android")
        return if (rid != 0) {
            val resourceId: Int =
                context.resources.getIdentifier("navigation_bar_height", "dimen", "android")
            context.resources.getDimensionPixelSize(resourceId)
        } else {
            0
        }
    }

}